﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using BW.MesProject.Public.ErrorCode;
using BW.MesProject.WareHouse.Domain;
using BW.MesProject.WareHouse.Infrastructure;
using BW.MesProject.WareHouse.Infrastructure.Dto.Input;
using System.Collections.Generic;
using BW.MesProject.WareHouse.Infrastructure.Dto.OutPut;

namespace BW.MesProject.WareHouse.Host.Controllers
{
    /// <summary>
    /// 审批流程节点
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProcessNodeController : ControllerBase
    {
        private readonly IProcessNodeServices ProcessNodeServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="processNodeServices"></param>
        public ProcessNodeController(IProcessNodeServices processNodeServices)
        {
            this.ProcessNodeServices = processNodeServices;
        }

        /// <summary>
        /// 添加审批流程节点及明细
        /// </summary>
        /// <param name="input">包含审批流程节点主表和明细的输入参数</param>
        /// <returns>添加结果</returns>
        [HttpPost]
        public ApiResult<int> AddProcessNodes([FromBody] ProcessNodeInput input)
        {
            return ProcessNodeServices.AddProcessNodes(input);
        }

        /// <summary>
        /// 添加审批流程
        /// </summary>
        /// <param name="approvalProcess">审批流程信息</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        public ApiResult<bool> AddApproveProcess([FromBody] ApprovalProcess approvalProcess)
        {
            return ProcessNodeServices.AddApproveProcess(approvalProcess);
        }

        /// <summary>
        /// 获取审批流程节点列表（分页）
        /// </summary>
        /// <param name="qryInput">查询参数</param>
        /// <param name="pnType">流程节点类型（可为空）</param>
        /// <returns>审批流程节点列表</returns>
        [HttpGet]
        public ApiResult<List<ProcessNodesOut>> GetProcessNodesList([FromQuery] QryInput qryInput, string pnType = null)
        {
            return ProcessNodeServices.GetProcessNodesList(qryInput, pnType);
        }

        /// <summary>
        /// 根据节点ID获取审批流程节点明细列表
        /// </summary>
        /// <param name="processNodesId">流程节点ID</param>
        /// <returns>审批流程节点明细列表</returns>
        [HttpGet]
        public ApiResult<List<ProcessNodesItem>> GetProcessNodesItemsByNodeId(string processNodesId)
        {
            return ProcessNodeServices.GetProcessNodesItemsByNodeId(processNodesId);
        }

        /// <summary>
        /// 获取审批流程节点明细单条记录
        /// </summary>
        /// <param name="nodeSn">节点序列号</param>
        /// <returns>审批流程节点明细信息</returns>
        [HttpGet]
        public ApiResult<ProcessNodesItem> GetProcessNodesItemById(string nodeSn)
        {
            return ProcessNodeServices.GetProcessNodesItemById(nodeSn);
        }

        /// <summary>
        /// 获取审批流程列表（分页）
        /// </summary>
        /// <param name="qryInput">查询参数</param>
        /// <param name="userId">用户ID</param>
        /// <returns>审批流程列表</returns>
        [HttpGet]
        public ApiResult<List<ApprovalProcess>> GetApprovalProcessList([FromQuery] QryInput qryInput, string userId)
        {
            return ProcessNodeServices.GetApprovalProcessList(qryInput, userId);
        }

        /// <summary>
        /// 获取审批流程列表（根据申请单ID）
        /// </summary>
        /// <param name="requisitionId">申请单ID</param>
        /// <returns>审批流程列表</returns>
        [HttpGet]
        public ApiResult<List<ApprovalProcess>> GetApprovalProcessByRId(string requisitionId)
        {
            return ProcessNodeServices.GetApprovalProcessByRId(requisitionId);
        }

        /// <summary>
        /// 获取审批流程的所有审批记录
        /// </summary>
        /// <param name="qryInput">查询参数</param>
        /// <param name="approvalProcessId">审批流程ID</param>
        /// <returns>审批记录列表</returns>
        [HttpGet]
        public ApiResult<List<ProcessRecords>> GetProcessRecordsByProcessId([FromQuery] QryInput qryInput, string approvalProcessId)
        {
            return ProcessNodeServices.GetProcessRecordsByProcessId(qryInput, approvalProcessId);
        }

        /// <summary>
        /// 修改审批流程节点及明细
        /// </summary>
        /// <param name="input">包含审批流程节点主表和明细的输入参数</param>
        /// <returns>修改结果</returns>
        [HttpPut]
        public ApiResult<bool> UpdateProcessNodes([FromBody] ProcessNodeInput input)
        {
            return ProcessNodeServices.UpdateProcessNodes(input);
        }

        /// <summary>
        /// 停用审批流程节点主表
        /// </summary>
        /// <param name="processNodes">流程节点信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public ApiResult<bool> UpdateProcessNodesEnable([FromBody] ProcessNodes processNodes)
        {
            return ProcessNodeServices.UpdateProcessNodesEnable(processNodes);
        }

        /// <summary>
        /// 取消审批流程
        /// </summary>
        /// <param name="approvalProcessId">审批流程ID</param>
        /// <param name="cancelReason">取消原因</param>
        /// <param name="operatorId">操作人ID</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public ApiResult<bool> CancelApprovalProcess(string approvalProcessId, string cancelReason, string operatorId)
        {
            return ProcessNodeServices.CancelApprovalProcess(approvalProcessId, cancelReason, operatorId);
        }

        /// <summary>
        /// 删除审批流程节点（包含相关的明细记录）
        /// </summary>
        /// <param name="processNodesId">流程节点ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete]
        public ApiResult<bool> DeleteProcessNode(string processNodesId)
        {
            return ProcessNodeServices.DeleteProcessNode(processNodesId);
        }

        /// <summary>
        /// 同意审批流程
        /// </summary>
        /// <param name="approval">审批流程信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public ApiResult<bool> ApproveProcess([FromBody] ApprovalProcess approval)
        {
            return ProcessNodeServices.ApproveProcess(approval);
        }

        /// <summary>
        /// 拒绝审批流程
        /// </summary>
        /// <param name="approval">审批流程信息</param>
        /// <returns>操作结果</returns>
        [HttpPut]
        public ApiResult<bool> RejectProcess([FromBody] ApprovalProcess approval)
        {
            return ProcessNodeServices.RejectProcess(approval);
        }
    }
}
